package top.jojoace.JPA.Controller;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import top.jojoace.GlobalExceptionAdvice_And_AutoURI.Exception.http.NotFoundException;
import top.jojoace.JPA.VO.PagingDozer;
import top.jojoace.JPA.VO.StudentSimplifyVo;
import top.jojoace.JPA.bo.PageCounter;
import top.jojoace.JPA.model_DDL.BothWay_OneToMany.BW_StudentItem;
import top.jojoace.JPA.service.StudentService;
import top.jojoace.JPA.util.CommonUtil;

import javax.validation.constraints.Positive;
import java.util.ArrayList;
import java.util.List;

@RestController
public class VOController {

    @Autowired
    private StudentService studentService;


    /**
     * 详细接口
     */
    @GetMapping("/id/{id}/detail")
    public BW_StudentItem getDetail(@PathVariable @Positive int id){
        BW_StudentItem student = this.studentService.selectBWStudentById(id);
        if (student == null){
            throw new NotFoundException(30003);
        }
        return student;
    }

    /**
     * 摘要接口
     *
     * 知识点：BeanUtils
     * 来源：org.springframework.beans.BeanUtils;
     * 特点：对象拷贝
     *
     * @Positive 正整数
     */
    @GetMapping("/id/{id}/simplify")
    public StudentSimplifyVo getSimplifyVo(@PathVariable @Positive int id){
        @Positive(message = "id必须是正整数")
        int id2 = 1;
        @Positive(message = "{id.positive}")
        int id3 = 3;

        BW_StudentItem student = this.studentService.selectBWStudentById(id);
        StudentSimplifyVo vo = new StudentSimplifyVo();

        BeanUtils.copyProperties(student,vo);

        return vo;
    }

    /**
     * 摘要列表
     *
     * 知识点：DozerBeanMapperBuilder、Mapperc
     * 来源：com.github.dozermapper.core.DozerBeanMapperBuilder;
     *      om.github.dozermapper.core.Mapper;
     * 特点：支持深拷贝
     *
     */
    //获得start到count的数据
    //latest?start= & count=
    @GetMapping("/latest")
    public List<StudentSimplifyVo> getLatestStudentList(@RequestParam(defaultValue = "0") Integer pageNumber,
                                                        @RequestParam(defaultValue = "10") Integer count){
        Mapper mapper = DozerBeanMapperBuilder.buildDefault();
        Page<BW_StudentItem> studentItems = this.studentService.getLatestPagingStudnets(pageNumber,count);

        List<StudentSimplifyVo> stuVOList = new ArrayList<>();
        studentItems.forEach(s->{
            StudentSimplifyVo simplifyVo = mapper.map(s,StudentSimplifyVo.class);
            stuVOList.add(simplifyVo);
        });

        return stuVOList;
    }


    /**
     * 摘要列表
     *
     * 知识点：BeanUtils
     */
    @GetMapping("/latest2")
    public List<StudentSimplifyVo> getLatestStudentList2(@RequestParam(defaultValue = "0") Integer start,
                                                        @RequestParam(defaultValue = "10") Integer count){
        Page<BW_StudentItem> studentItems = this.studentService.getLatestPagingStudnets(start,count);
        List<StudentSimplifyVo> stuVOList = new ArrayList<>();

        studentItems.forEach(item->{
            StudentSimplifyVo simplifyVo = new StudentSimplifyVo();
            BeanUtils.copyProperties(item,simplifyVo);
            stuVOList.add(simplifyVo);
        });

        return stuVOList;
    }

    /**
     * 使用pageCounter、Paging
     * @param start
     * @param count
     * @return
     */
    @GetMapping("/latest3?start= &count=")
    public PagingDozer<BW_StudentItem,StudentSimplifyVo> getLatestStudentList3(@RequestParam(defaultValue = "0") Integer start,
                                                         @RequestParam(defaultValue = "10") Integer count){
        PageCounter pageCounter = CommonUtil.converToPageParameter(start, count);
        Page<BW_StudentItem> studentItems = this.studentService.getLatestPagingStudnets(pageCounter.getPage(),pageCounter.getCount());

        return new PagingDozer<BW_StudentItem,StudentSimplifyVo>(studentItems,StudentSimplifyVo.class);
    }
}
